import uuid from 'uuid';
import query from './atom/db/query';
import update from './atom/db/update';
import { IBaseTable } from './atom/db/interfaces';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	user_code: string; s_screen_no: number; e_screen_no: number; no: string;
}

interface deskScreen extends IBaseTable {
	no: string; user_code: string; user_name: string; screen_no: number;
}

async function atom(msg: Message) {
	const systemid = '6051';

	const q = query(action_id, session_id, spaceid, systemid);
	q.prepare('desk_screen', ['screen_no', '_id', 'no'], { screen_no: { $lte: msg.e_screen_no, $gte: msg.s_screen_no } });
	// 桌面分屏
	const [data] = await q.exec();

	const upd_data: any[] = [];
	data.forEach((d: deskScreen) => {
		const no = d.no;
		if (no === msg.no) {
			d.screen_no = msg.s_screen_no;
		} else {
			d.screen_no = d.screen_no + 1;
		}
		upd_data.push(d);
	});

	const { exec, prepare } = update(action_id, session_id, spaceid, systemid);

	// 桌面分屏
	prepare('desk_screen', upd_data);
	const res = await exec();

	if (res.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}